package com.lindroy.androidplayer.ui.collection

import android.os.Bundle
import android.view.View
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentCollectionListBinding
import com.lindroy.androidplayer.ui.adapter.CollectedArticleAdapter
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.utils.collectListWhenResumed
import com.lindroy.androidplayer.viewmodels.CollectedArticleViewModel

/**
 * @author Lin
 * @date 2022/2/18
 * @function 我的收藏-文章
 */
class CollectionArticlesFragment :
    BaseListFragment<FragmentCollectionListBinding, CollectedArticleViewModel>() {

    private val adapter by lazy { CollectedArticleAdapter(viewLifecycleOwner) }

    override fun FragmentCollectionListBinding.initView(savedInstanceState: Bundle?) {
        vm = mViewMode
        rvArticle.addSimpleDivider()
        rvArticle.adapter = adapter.withFooter()
    }

    override fun FragmentCollectionListBinding.initListener() {
        mViewMode.setLayoutStateFlow(adapter.loadStateFlow)
        mViewMode.collectionArticleList.collectListWhenResumed(viewLifecycleOwner) {
            adapter.submitData(it)
        }
        mViewMode.itemClickLiveData.observe(viewLifecycleOwner){
            WebViewFragment.start(this@CollectionArticlesFragment,it.id,it.link,it.title,true)
        }
    }

    override fun onRefresh() {
        adapter.refresh()
    }

    override fun onRetry(view: View) {
        adapter.refresh()
    }

}